﻿using System;
using System.Collections.Generic;
using System.Text;
using Entity;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Service
{
    public class Pick_51job
    {
        public static List<Company> Pick_Page(Task _task,string htmlStr,ToolStripProgressBar _planBar)
        {
            List<Company> list = new List<Company>();
            if (!htmlStr.Contains("tr0"))
            {
                return list;
            }
            string[] items = Clear_HTML(htmlStr);
            _planBar.Maximum = items.Length;
            _planBar.Value = 0;
            foreach (string itemStr in items)
            {
                _planBar.Value++;
                string item = itemStr;
                Company c = new Company();
                item = item.Substring(item.IndexOf("td1") + 3);
                item = item.Substring(0, item.IndexOf("td3") - 3);
                c.City = _task.City.Name;
                c.JobList = Get_JobList(item);
                if (!c.JobList.Contains(_task.Keyword.Name))
                {
                    continue;
                }
                c.Name = Get_CompanyName(item);
                c.URL = Get_CompanyURL(item);
                c.JobURL = Get_JobURL(item);
                Set_Contact(c, c.JobURL);
                if (string.IsNullOrEmpty(c.PhoneNumber))
                {
                    continue;
                }
                c.Keyword = _task.Keyword.Name;
                DAL.DAL_Service.Company_Add(c);
                list.Add(c);
            }
            return list;    
        }

        //将页面HTML源码去除无用部分并切割出有用部分的序列
        private static string[] Clear_HTML(string htmlStr)
        {
            //裁剪首
            htmlStr = htmlStr.Substring(htmlStr.IndexOf("titleTr") + 7);
            htmlStr = htmlStr.Substring(htmlStr.IndexOf("tr0") + 3);
            //裁剪尾
            htmlStr = htmlStr.Substring(0, htmlStr.IndexOf("bt_center clearboth") - 19);
            htmlStr = htmlStr.Substring(0, htmlStr.LastIndexOf("tr3") - 3);
            string[] items = htmlStr.Split(new string[] { "tr0" }, StringSplitOptions.RemoveEmptyEntries);
            return items;
        }
        #region Get_搜索列表页
        private static string Get_CompanyName(string item)
        {
            item = item.Substring(item.IndexOf("class=\"coname\""));
            item = item.Substring(item.IndexOf(">") + 1);
            item = item.Substring(0, item.IndexOf("<"));
            return Common.HTMLSourceDispose.ClearHTMLTags(item);
        }

        private static string Get_CompanyURL(string item)
        {
            item = item.Substring(item.IndexOf("td2\""));
            item = item.Substring(item.IndexOf("href=") + 6);
            item = item.Substring(0, item.IndexOf("\""));
            return item;
        }

        private static string Get_JobURL(string item)
        {
            item = item.Substring(item.IndexOf("a href=\"") + 8);
            item = item.Substring(0, item.IndexOf("\""));
            return item;
        }

        private static string Get_JobList(string item)
        {
            item = item.Substring(item.IndexOf("_blank\""));
            item = item.Substring(item.IndexOf(">") + 1);
            item = item.Substring(0, item.IndexOf("</a>"));
            return Common.HTMLSourceDispose.ClearHTMLTags(item);
        } 
        #endregion

        #region 获取联系方式
        /// <summary>
        /// 在职位详情页抓取联系方式、公司规模、公司行业等信息。
        /// </summary>
        /// <param name="c"></param>
        /// <param name="jobURL"></param>
        private static void Set_Contact(Company c, string jobURL)
        {
            string htmlStr = Common.RequestHelper.DoGet(jobURL, Encoding.GetEncoding("gb2312"));

            htmlStr = htmlStr.Substring(htmlStr.IndexOf("公司行业：") + 5);
            c.Profession = Get_Profession(htmlStr);
            htmlStr = htmlStr.Substring(htmlStr.IndexOf("公司规模：") + 5);
            c.PeopleCount = Get_PeopleCount(htmlStr);
            htmlStr = htmlStr.Substring(htmlStr.IndexOf("职位描述") + 5);
            //保存包含公司相关信息的源码
            string companyInfoStr = htmlStr.Substring(htmlStr.IndexOf("http://img01.51jobcdn.com/im/2009/search/c/gsjjbt.gif") + 35);
            companyInfoStr = companyInfoStr.Substring(0, companyInfoStr.IndexOf("jobs_com") - 8);
            //继续裁剪，仅剩职位描述
            htmlStr = htmlStr.Substring(0, htmlStr.IndexOf("txt_4") - 5);
            //首先在职位描述中找
            c.Email = Get_Email(htmlStr);
            c.PhoneNumber = Get_PhoneNum(htmlStr);
            c.Linkman = Get_Linkman(htmlStr);
            c.Website = Get_Websit(htmlStr);
            c.Address = Get_Address(htmlStr);
            if (string.IsNullOrEmpty(c.Email))
            {
                c.Email = Get_Email(companyInfoStr);
            }
            if (string.IsNullOrEmpty(c.PhoneNumber))
            {
                c.PhoneNumber = Get_PhoneNum(companyInfoStr);
            }
            if (string.IsNullOrEmpty(c.Linkman))
            {
                c.Linkman = Get_Linkman(companyInfoStr);
            }
            if (string.IsNullOrEmpty(c.Website))
            {
                c.Website = Get_Websit(companyInfoStr);
            }
            if (string.IsNullOrEmpty(c.Address))
            {
                c.Address = Get_Address(companyInfoStr);
            }
        }

        private static string Get_Address(string companyInfoStr)
        {
            string[] keys = new string[] { "公司地址：","地址：", "地&nbsp;&nbsp;&nbsp;&nbsp;址：", "深圳地址：" };
            foreach (string key in keys)
            {
                if (companyInfoStr.Contains(key))
                {
                    companyInfoStr = companyInfoStr.Substring(companyInfoStr.IndexOf(key) + key.Length);
                    companyInfoStr = companyInfoStr.Substring(0, companyInfoStr.IndexOf("<"));
                    return Common.HTMLSourceDispose.ClearHTMLTags(companyInfoStr.Trim());
                }
            }
            return "";
        }

        private static string Get_Websit(string companyInfoStr)
        {
            string[] keys = new string[] { "公司网站：<a", "公司网站：", "公司主页：" };
            foreach (string key in keys)
            {
                if (companyInfoStr.Contains(key))
                {
                    companyInfoStr = companyInfoStr.Substring(companyInfoStr.IndexOf(key) + key.Length);
                    companyInfoStr = companyInfoStr.Substring(0, companyInfoStr.IndexOf("<"));
                    if (companyInfoStr.Contains(">"))
                    {
                        companyInfoStr = companyInfoStr.Substring(companyInfoStr.IndexOf(">") + 1);
                    }
                    return Common.HTMLSourceDispose.ClearHTMLTags(companyInfoStr.Trim());
                }
            }
            return "";
        }

        private static string Get_Linkman(string companyInfoStr)
        {
            string[] keys = new string[] { "联系人：", "联&nbsp;系&nbsp;人：" };
            foreach (string key in keys)
            {
                if (companyInfoStr.Contains(key))
                {
                    companyInfoStr = companyInfoStr.Substring(companyInfoStr.IndexOf(key) + key.Length);
                    companyInfoStr = companyInfoStr.Substring(0,companyInfoStr.IndexOf("<"));
                    return Common.HTMLSourceDispose.ClearHTMLTags(companyInfoStr.Trim());
                }
            }
            return "";
        }

        #region Get_职位详情页
        private static string Get_Email(string htmlStr)
        {
            string email = string.Empty;
            Regex r = new Regex(@"(\w)+(\.\w+)*@(\w)+((\.\w+)+)");
            Match m = r.Match(htmlStr);
            if (m.Success)
            {
                email = m.Value;
            }
            return email;
        }

        private static string Get_PhoneNum(string htmlStr)
        {
            string phoneNum = string.Empty;
            string[] regs = new string[] { @"(0755)\d{7,8}",@"0755[\-]\d{7,8}", @"0755\d{7,8}", @"1[3458]\d{9}", @"\d{8}" };
            foreach (string reg in regs)
            {
                Regex r = new Regex(reg);
                Match m = r.Match(htmlStr);
                if (m.Success)
                {
                    if (!phoneNum.Contains(m.Value))
                    {
                        phoneNum += "@" + m.Value;
                    }
                }
            }
            return phoneNum;
        }
        private static string Get_PeopleCount(string htmlStr)
        {
            htmlStr = htmlStr.Substring(0, htmlStr.IndexOf("</td>"));
            htmlStr = Common.HTMLSourceDispose.ClearHTMLTags(htmlStr);
            return htmlStr;
        }

        private static string Get_Profession(string htmlStr)
        {
            htmlStr = htmlStr.Substring(0, htmlStr.IndexOf("<strong>"));
            htmlStr = Common.HTMLSourceDispose.ClearHTMLTags(htmlStr);
            return htmlStr;
        }  
        #endregion
        #endregion
    }
}
